package com.ty.mapper;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ty.entity.UserEntity;
import com.ty.req.UserUpdateReq;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 *
 * @author : txy
 * @create : 2023/12/28 16:17
 */
public interface UserMapper extends BaseMapper<UserEntity> {

    @Update("update user_tb set password = #{newPassword} where id =#{id} and password = #{password}")
    Integer updatePassword(UserUpdateReq sysUser);

    @Update("update user_tb set phone = #{newPhone} where id =#{id} and password = #{phone}")
    Integer updatePhone(UserUpdateReq sysUser);

    @Select("select distinct u.* from user_tb u left join user_address ua on u.id = ua.user_id ${ew.customSqlSegment}")
    @Results({
            //因为type_id字段拿去关联外表，想要赋值给bookinfo还要再映射一次
            @Result(property = "id", column = "id"),
            @Result(property = "addresses", column = "id",
                    many = @Many(select = "com.ty.mapper.UserAddressMapper.selectByUserId"))
    })
    List<UserEntity> selectAll(Page<UserEntity> page, @Param(Constants.WRAPPER) Wrapper<UserEntity> wrapper);
}
